package day12;
//volatile禁止重排序
public class Demo3 {
    static volatile int a,b,c,d;
    public static void main(String[] args) throws InterruptedException {
        int i=0;//计数得
        while(true){
            i++;//计数
            a=b=c=d=0;//每循环一次 数据清0
            Thread t1=new Thread(()->{
                a=1; //1
                c=b; //2
            });
            Thread t2=new Thread(()->{
                b=1; //1
                d=a; //2
            });
            t1.start();
            t2.start();

            t1.join();
            t2.join();
            if(c==0 && d==0){//重排序了
                System.out.println("执行第"+i+"次出现了重排序");
                break;
            }else{
                System.out.println(i);
            }
        }
    }
}

